﻿package mortal.game.manager
{
    import com.gengine.global.*;
    import flash.display.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.layer3D.*;
    import mortal.game.view.chat.*;

    public class LayerManager extends Object
    {
        public static var sceneLayer:UILayer;
        public static var npcDialogLayr:NPCDailogLayer;
        public static var uiLayer:MainUILayer;
        public static var entityTalkLayerParent:UILayer;
        public static var entityTalkLayer:EntityTalkLayer;
        public static var smallIconLayer:SmallIconLayer;
        public static var actionIconLayer:ActionIconLayer;
        public static var chatIconLayer:ChatIconLayer;
        public static var specialIconLayer:SpecialIconLayer;
        public static var entityTalkLayerTemp:UILayer;
        public static var windowLayer:WindowLayer;
        public static var windowLayer3D:WindowLayer;
        public static var fullScreenLayer:FullScreenLayer;
        public static var fullScreenTopLayer:WindowLayer;
        public static var topLayer:UILayer;
        public static var popupLayer:UILayer;
        public static var guideLayer:UILayer;
        public static var alertLayer:UILayer;
        public static var dragLayer:UILayer;
        public static var toolTipLayer:UILayer;
        public static var msgTipLayer:UILayer;
        public static var flowersLayer:UILayer;
        public static var dramaMovieLayer:UILayer;
        public static var highestLayer:UILayer;
        private static var _topWindowLayer:WindowLayer;
        private static var _uiMask:DisplayObject;
        private static var _windowMask:DisplayObject;

        public function LayerManager()
        {
            return;
        }// end function

        public static function init() : void
        {
            sceneLayer = new UILayer();
            sceneLayer.tabChildren = false;
            Global.application.addChild(sceneLayer);
            uiLayer = new MainUILayer();
            uiLayer.tabChildren = false;
            uiLayer.mouseEnabled = false;
            Global.application.addChild(uiLayer);
            MsgManager.addTipStage(uiLayer);
            entityTalkLayerParent = new UILayer();
            entityTalkLayerParent.tabChildren = false;
            entityTalkLayerParent.mouseEnabled = false;
            uiLayer.addPopUp(entityTalkLayerParent);
            entityTalkLayer = new EntityTalkLayer();
            entityTalkLayer.tabChildren = false;
            entityTalkLayer.mouseEnabled = false;
            entityTalkLayerParent.addPopUp(entityTalkLayer);
            npcDialogLayr = new NPCDailogLayer();
            npcDialogLayr.tabChildren = false;
            npcDialogLayr.mouseEnabled = false;
            uiLayer.addPopUp(npcDialogLayr);
            actionIconLayer = new ActionIconLayer();
            uiLayer.addPopUp(actionIconLayer);
            entityTalkLayerTemp = new UILayer();
            entityTalkLayerTemp.tabChildren = false;
            entityTalkLayerTemp.mouseEnabled = false;
            Global.application.addChild(entityTalkLayerTemp);
            windowLayer = new WindowLayer();
            windowLayer.tabChildren = false;
            Global.application.addChild(windowLayer);
            windowLayer3D = new WindowLayer();
            windowLayer3D.tabChildren = false;
            Global.application.addChild(windowLayer3D);
            _topWindowLayer = windowLayer3D;
            fullScreenLayer = new FullScreenLayer();
            fullScreenLayer.tabChildren = false;
            Global.application.addChild(fullScreenLayer);
            fullScreenTopLayer = new WindowLayer();
            fullScreenTopLayer.tabChildren = false;
            Global.application.addChild(fullScreenTopLayer);
            topLayer = new UILayer();
            topLayer.tabChildren = false;
            Global.application.addChild(topLayer);
            smallIconLayer = new SmallIconLayer();
            smallIconLayer.tabChildren = false;
            smallIconLayer.mouseEnabled = false;
            topLayer.addChild(smallIconLayer);
            chatIconLayer = new ChatIconLayer();
            chatIconLayer.tabChildren = false;
            chatIconLayer.mouseEnabled = false;
            uiLayer.addPopUp(chatIconLayer);
            specialIconLayer = new SpecialIconLayer();
            specialIconLayer.tabChildren = false;
            specialIconLayer.mouseEnabled = false;
            uiLayer.addPopUp(specialIconLayer);
            popupLayer = new UILayer();
            popupLayer.tabChildren = false;
            Global.application.addChild(popupLayer);
            guideLayer = new UILayer();
            guideLayer.tabChildren = false;
            Global.application.addChild(guideLayer);
            dragLayer = new UILayer();
            dragLayer.tabChildren = false;
            Global.application.addChild(dragLayer);
            msgTipLayer = new UILayer();
            msgTipLayer.tabChildren = false;
            Global.application.addChild(msgTipLayer);
            MsgManager.addBroadStage(msgTipLayer);
            toolTipLayer = new UILayer();
            toolTipLayer.tabChildren = false;
            Global.application.addChild(toolTipLayer);
            alertLayer = new UILayer();
            alertLayer.tabChildren = false;
            Global.application.addChild(alertLayer);
            flowersLayer = new UILayer();
            Global.application.addChild(flowersLayer);
            dramaMovieLayer = new UILayer();
            Global.application.addChild(dramaMovieLayer);
            highestLayer = new UILayer();
            Global.application.addChild(highestLayer);
            CursorManager.init();
            return;
        }// end function

        public static function isHasFullSceenOpen() : Boolean
        {
            return fullScreenLayer.numChildren != 0;
        }// end function

        public static function get topWindowLayer() : WindowLayer
        {
            return _topWindowLayer;
        }// end function

        public static function get bottomWindowLayer() : WindowLayer
        {
            if (_topWindowLayer == windowLayer)
            {
                return windowLayer3D;
            }
            return windowLayer;
        }// end function

        public static function setWindowLayerTop(param1:WindowLayer) : void
        {
            if (param1 == windowLayer3D || param1 == windowLayer)
            {
                if (param1 != _topWindowLayer)
                {
                    Global.application.swapChildren(windowLayer, windowLayer3D);
                    _topWindowLayer = param1;
                    updateMask();
                }
            }
            return;
        }// end function

        public static function removeAllWindows() : void
        {
            windowLayer.removeAllWindow();
            windowLayer3D.removeAllWindow();
            fullScreenLayer.removeAllWindow();
            fullScreenTopLayer.removeAllWindow();
            npcDialogLayr.removeAllWindow();
            return;
        }// end function

        public static function setWindowSpriteMask(param1:DisplayObject = null, param2:DisplayObject = null) : void
        {
            if (_uiMask == param1 && _windowMask == param2)
            {
                return;
            }
            _uiMask = param1;
            _windowMask = param2;
            updateMask();
            return;
        }// end function

        private static function updateMask() : void
        {
            if (_uiMask)
            {
                uiLayer.mask = _uiMask;
                _uiMask.visible = true;
            }
            else
            {
                if (uiLayer.mask)
                {
                    uiLayer.mask.visible = false;
                }
                if (_uiMask)
                {
                    _uiMask.visible = false;
                }
                uiLayer.mask = null;
            }
            if (_windowMask && _topWindowLayer == windowLayer3D)
            {
                windowLayer.mask = _windowMask;
                _windowMask.visible = true;
            }
            else
            {
                if (windowLayer.mask)
                {
                    windowLayer.mask.visible = false;
                }
                if (_windowMask)
                {
                    _windowMask.visible = false;
                }
                windowLayer.mask = null;
            }
            return;
        }// end function

        public static function changeEntityTalkLayerToUILayer(param1:Boolean) : void
        {
            if (param1)
            {
                entityTalkLayerParent.addPopUp(entityTalkLayer);
            }
            else
            {
                entityTalkLayerTemp.addPopUp(entityTalkLayer);
            }
            return;
        }// end function

        public static function nomalUiLayerHide() : void
        {
            sceneLayer.visible = false;
            npcDialogLayr.visible = false;
            uiLayer.visible = false;
            windowLayer.visible = false;
            return;
        }// end function

        public static function nomalUiLayerShow() : void
        {
            sceneLayer.visible = true;
            npcDialogLayr.visible = true;
            uiLayer.visible = true;
            windowLayer.visible = true;
            (GameController.chat.view as ChatModule).refresh();
            return;
        }// end function

    }
}
